package com.migu.hbase

import org.apache.hadoop.hbase.TableName
import org.apache.hadoop.hbase.client.{Connection, Put, Table}
import org.apache.hadoop.hbase.util.Bytes

/**
  * Created by maguoyun on 2017/03/21.
  */
object OnlineHBase {
  private val dao: OnlineHBase = new OnlineHBase()

  def apply: OnlineHBase = dao
}


class OnlineHBase private extends Serializable {

  @transient val ptn = "cdn:online_user_monitor"    //表名
  @transient val ppvcf = "ouinfo"   //ou列簇
  @transient val pcqn = "count"     //列

  def insertOnlinePeople(rk: String, ou: String): Unit = {
    var con: Connection = null
    var table: Table = null
    try {
      con = HbUtil.getConn()  //获取连接
      table = con.getTable(TableName.valueOf(ptn))  //table_name:"cdn:online_user_monitor"
      val put = new Put(Bytes.toBytes(rk))  //rowkey
      //向HBase表中插入数据
      put.addColumn(Bytes.toBytes(ppvcf), Bytes.toBytes(pcqn), Bytes.toBytes(ou)) //family,column,value
      table.put(put)
    } catch {
      case e: Exception => println("insert ou error ................ " + e.printStackTrace())
    } finally {
      if (table != null) table.close()
      if (con != null) con.close()
    }
  }
}
